package com.googlecode.economini.issues.task;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.concurrent.Callable;

import com.googlecode.economini.converter.ArticleConverter;
import com.googlecode.economini.issues.IssueStore;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.net.Uri;

public class LoadArticleTask implements Callable<Object> {

  private final ContentResolver resolver;
  private final Uri issueUri;
  private final long issueId;
//  private final String section;
  private final String url;
  private final String title;

  public LoadArticleTask(
      ContentResolver resolver,
      Uri issueUri,
      long issueId,
      String section,
      String url,
      String title) {
    this.resolver = resolver;
    this.issueUri = issueUri;
    this.issueId = issueId;
//    this.section = section;
    this.url = url;
    this.title = title;
  }

  @Override
  public Object call() throws Exception {
    ContentValues values = new ContentValues();
    values.put(IssueStore.ARTICLE_TITLE_COLUMN, title);
    values.put(IssueStore.ARTICLE_URL_COLUMN, url);
    values.put(IssueStore.ARTICLE_ISSUE_ID_COLUMN, issueId);

    resolver.insert(issueUri, values);

    InputStream inputStream = new URL(
        "http://www.economist.com" + url).openStream();
    try {
      processArticle(new BufferedInputStream(inputStream));
    } finally {
      inputStream.close();
    }

    return null;
  }

  private void processArticle(InputStream inputStream) {
    ArticleConverter articleConverter = ArticleConverter.newInstance();
    
  }
}
